




//
//  GuiGeXinghaoViewController.m
//  nncloud
//
//  Created by macfeel on 2019/5/28.
//  Copyright © 2019 macfai. All rights reserved.
//

#import "GuiGeXinghaoViewController.h"

@interface GuiGeXinghaoViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *equipArray;

@property(nonatomic,strong)UITextField *textField;

@property(nonatomic,assign)NSInteger currentPage;

@end

@implementation GuiGeXinghaoViewController
#define cellIdentifier  @"relayd"
//#define HeaderHight  290
#define PageSize 20

-(UITableView *)tableView{
    
    if (!_tableView) {
        
        WeakStament(ws);
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - ScreenHeightFlex) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 55;
        //        _tableView.separatorColor = [UIColor clearColor];
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
                _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                    [ws refresh];
                }];
                _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                    [ws loadMoreData];
                }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    ///设备型号  也是共用一个页面
    
    [self.navigationItem setItemWithTitle:@"设备型号" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.equipArray = [NSMutableArray array];
    self.currentPage = 1;
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 35, 35) title:@"新增型号" Image:@"" selectedIMG:@"" tartget:self action:@selector(addNew)];
    
    [self.view addSubview:self.tableView];
    [self loadData];
    // Do any additional setup after loading the view.
}
///新增名称
-(void)addNew{
    ///
    [self addMingcheng];
}

///新增名称
-(void)addMingcheng
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请添加名称" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    self.textField = [UITextField new];
    self.textField.placeholder = @"请输入";
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = self.textField.placeholder;
        textField.keyboardType = 0;
        self.textField = textField;
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        return ;
    }];
    UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        //        [self addManufacturer];
        [self saveAdd];
    }];
    
    [alertController addAction:cancelAction];
    [alertController addAction:otherAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
}

#pragma mark - 添加型号接口
///保存新增
-(void)saveAdd{
    
    
    NSString *url;
    NSDictionary *dict;
    if ([[NUserModel shareInstance].equipType isEqualToString:@"r"]) {
        
        ///relayType 传的是对应的 序号或者id 是数字  和转辙机不一样
        url = kAddRelayXinghaoUrl;
         dict = @{@"relayType":[NUserModel shareInstance].relayName?:@"",@"relayModel":self.textField.text?:@"",};
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"s"]){
        ///转辙机
        url = kAddSwitchXinghaoUrl;
        ///  relayType 设备所属大类名称   relayModel  具体型号
        ///relayType 传的是对应的汉字  这个和继电器不一样
        dict = @{@"relayType":[NUserModel shareInstance].switchName?:@"",@"relayModel":self.textField.text?:@"",};
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"c"]){
        ///
        url = kAddCMXinghaoUrl;
        dict = @{@"relayType":[NUserModel shareInstance].CMName?:@"",@"relayModel":self.textField.text?:@"",};
    }else{
        url = kAddCamelXinghaoUrl;
        dict = @{@"relayType":[NUserModel shareInstance].camelName?:@"",@"relayModel":self.textField.text?:@"",};
    }
    
    
    [NNetworkClient postRequestWithBaseUrl:url setParameters:dict success:^(id responseObject) {
        if ([responseObject[@"status"] integerValue]==0) {
            [AFToast showText:@"添加成功"];
        }
    } fail:^(NSError *error) {
    } delegater:nil];
}


#pragma mark - 获取型号列表 接口

////根据继电器的名称 选择对应名称下的型号
-(void)loadData{
    
    ///规格型号列表
    ///是根据某一类型作为参数，根据这个参数获取该继电器下面的所有型号
    NSString *url;
    NSDictionary *dict;
    if (self.currentPage==1) {
        [self.equipArray removeAllObjects];
    }
    
    if ([[NUserModel shareInstance].equipType isEqualToString:@"r"]) {
        
        url = kGetRelayXinghaoListUrl;
        dict = @{@"relaysType":[NUserModel shareInstance].relayName?:@"",@"currentPage":@(self.currentPage)};
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"s"]){
        ///转辙机
        url = kGetSwitchXinghaoListUrl;
        dict = @{@"relaysType":[NUserModel shareInstance].switchName?:@"",@"currentPage":@(self.currentPage)};
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"c"]){
        
        ///综合器材
        url = kGetCMXinghaoListUrl;
        dict = @{@"relaysType":[NUserModel shareInstance].CMName?:@"",@"currentPage":@(self.currentPage)};
    }else{
        
        url = kGetCamelXinghaoListUrl;
        dict = @{@"relaysType":[NUserModel shareInstance].camelName?:@"",@"currentPage":@(self.currentPage)};
    }
    
    
    [NNetworkClient postRequestWithBaseUrl:url setParameters:dict success:^(id responseObject) {
        
        YYLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] integerValue]==0) {
            NSArray *arr = responseObject[@"data"][@"lists"];
            
            
            for (NSDictionary *dict in arr) {
                CommonModel *model = [CommonModel mj_objectWithKeyValues:dict];
                [self.equipArray addObject:model];
            }
            [self.tableView reloadData];
            [self endRefresh];
            
            
            if (arr.count==0) {
                [self noMoreData];
            }
        }
        
    } fail:^(NSError *error) {
          [self endRefresh];
    } delegater:nil];
}

///刷新
- (void)refresh {
    self.currentPage = 1;
    [self loadData];
}
///加载更多
- (void)loadMoreData{
    self.currentPage++;
    [self loadData];
}
- (void)endRefresh {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)noMoreData {
    [self.tableView reloadData];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
}

#pragma mark - tableView   delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.equipArray.count;
    //    NSDictionary *dict = self.dict;
    //    return [dict allKeys].count;
    //    return self.nameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommonModel *model = self.equipArray[indexPath.row];
    //    NSDictionary *dict = self.dict;
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: cellIdentifier];
    }

    cell.selectionStyle = 0;
    cell.textLabel.textColor = kDarkGrayColor;
    
    if ([[NUserModel shareInstance].equipType isEqualToString:@"r"]) {
        
         cell.textLabel.text = model.xinghao;
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"s"]){
        ///转辙机 和 继电器的叫法不一样  叫 器材型号
         cell.textLabel.text = model.qicaiXinghao;
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"c"]){
        ///
        
        cell.textLabel.text = model.xinghao;
    }else{
        cell.textLabel.text = model.qicaiXinghao;
    }
    
   
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommonModel *model = self.equipArray[indexPath.row];
    if ([[NUserModel shareInstance].equipType isEqualToString:@"r"]) {
        
        if (self.block) {
            self.block(model.xinghao);
        }
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"s"]){
        ///转辙机
        if (self.block) {
            ///传的是名称
            self.block(model.qicaiXinghao);
        }
        
    }else if ([[NUserModel shareInstance].equipType isEqualToString:@"c"]){
        ///综合器材
        if (self.block) {
            ///传的是型号
            self.block(model.xinghao);
        }
        
    }else{
        if (self.block) {
            ///传的是型号
            self.block(model.qicaiXinghao);
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}


@end
